package controller

import (
	"back/app/config"
	"back/app/model"
	"back/app/utils"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"net/http"
	"strconv"
)

type User struct {
	warp
}

func (s *User) LoginHandler(c *gin.Context) {
	cont, _ := ioutil.ReadAll(c.Request.Body)
	if cont == nil {
		config.GetLogger().Warnw("账号密码不能为空",
			"cont", cont,
		)
		c.JSON(http.StatusOK, s.FailWarp("账号密码不能为空"))
		return
	}

	user := model.NewUser()

	err, data := user.GetLoginData(string(cont))
	if err != nil {
		config.GetLogger().Warnw("登录失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	login := new(model.ReceiveLogin)
	err = json.Unmarshal(cont, &login)

	jwtUtil := utils.JWT{}
	jwt, err := jwtUtil.Auth(login.UserName, data.UserID, data.Type)

	if err != nil {
		config.GetLogger().Warnw("jwt签发失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp("jwt签发失败"))
		return
	}

	data.JWT = jwt

	c.JSON(http.StatusOK, s.SuccessWarp(data))
}

func (s *User) RegisterHandler(c *gin.Context) {

	cont, _ := ioutil.ReadAll(c.Request.Body)

	if cont == nil {
		config.GetLogger().Warnw("账户信息不能为空",
			"cont:", cont,
		)
		c.JSON(http.StatusOK, s.FailWarp("账号信息不能为空"))
		return
	}

	user := model.NewUser()

	err, data := user.GetRegisterData(string(cont))
	if err != nil {
		config.GetLogger().Warnw("注册数据查询失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))
}

func (s *User) SecureCodeHandler(c *gin.Context) {
	tel, _ := ioutil.ReadAll(c.Request.Body)

	if tel == nil {
		config.GetLogger().Warnw("手机号码不能为空",
			"tel:", tel,
		)
		c.JSON(http.StatusOK, s.FailWarp("手机号码不能为空"))
		return
	}

	user := model.NewUser()

	err, data := user.GetSecureCodeData(string(tel))
	if err != nil {
		config.GetLogger().Warnw("发送手机验证码失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))
}

func (s *User) VerifyCodeHandler(c *gin.Context) {
	cont, _ := ioutil.ReadAll(c.Request.Body)

	if cont == nil {
		config.GetLogger().Warnw("信息不能为空",
			"cont:", cont,
		)
		c.JSON(http.StatusOK, s.FailWarp("信息不能为空"))
		return
	}

	user := model.NewUser()

	err, data := user.GetVerifyCodeData(string(cont))
	if err != nil {
		config.GetLogger().Warnw("验证手机验证码失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))
}

func (s *User) UserInfoHandler(c *gin.Context) {
	userID, _ := ioutil.ReadAll(c.Request.Body)

	if userID == nil {
		config.GetLogger().Warnw("用户ID不能为空",
			"userID:", userID,
		)
		c.JSON(http.StatusOK, s.FailWarp("用户ID不能为空"))
		return
	}

	user := model.NewUser()

	err, data := user.GetUserInfoData(string(userID))
	if err != nil {
		config.GetLogger().Warnw("获取用户基本信息失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))
}

func (s *User) ChangeInfoHandler(c *gin.Context) {
	cont, _ := ioutil.ReadAll(c.Request.Body)

	if cont == nil {
		config.GetLogger().Warnw("用户信息不能为空",
			"cont:", cont,
		)
		c.JSON(http.StatusOK, s.FailWarp("用户信息不能为空"))
		return
	}

	user := model.NewUser()

	err, data := user.GetChangeData(string(cont))
	if err != nil {
		config.GetLogger().Warnw("更新用户基本信息失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))
}

func (s *User) ChangePwdHandler(c *gin.Context) {
	cont, _ := ioutil.ReadAll(c.Request.Body)

	if cont == nil {
		config.GetLogger().Warnw("用户信息不能为空",
			"cont:", cont,
		)
		c.JSON(http.StatusOK, s.FailWarp("用户信息不能为空"))
		return
	}

	user := model.NewUser()

	err, data := user.GetChangePwdData(string(cont))
	if err != nil {
		config.GetLogger().Warnw("修改密码失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))
}

func (s *User) ChangePwdHandler2(c *gin.Context) {
	cont, _ := ioutil.ReadAll(c.Request.Body)

	if cont == nil {
		config.GetLogger().Warnw("用户信息不能为空",
			"cont:", cont,
		)
		c.JSON(http.StatusOK, s.FailWarp("用户信息不能为空"))
		return
	}

	user := model.NewUser()

	err, data := user.GetChangePwdData2(string(cont))
	if err != nil {
		config.GetLogger().Warnw("修改密码失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))
}

func (s *User) MovementListHandler(c *gin.Context) {
	cont, _ := ioutil.ReadAll(c.Request.Body)

	if cont == nil {
		config.GetLogger().Warnw("信息不能为空",
			"cont:", cont,
		)
		c.JSON(http.StatusOK, s.FailWarp("信息不能为空"))
		return
	}

	user := model.NewUser()

	err, data := user.GetMovementListData(string(cont))
	if err != nil {
		config.GetLogger().Warnw("查询用户动作信息列表失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))
}

func (s *User) MovementAmpPhaseHandler(c *gin.Context) {
	cont, _ := ioutil.ReadAll(c.Request.Body)

	if cont == nil {
		config.GetLogger().Warnw("信息不能为空",
			"cont:", cont,
		)
		c.JSON(http.StatusOK, s.FailWarp("信息不能为空"))
		return
	}

	user := model.NewUser()

	err, data := user.GetAPData(string(cont))
	if err != nil {
		config.GetLogger().Warnw("查询用户动作信息失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))
}

func (s *User) HeadPortraitListHandler(c *gin.Context) {

	user := model.NewUser()

	err, data := user.GetHeadPortraitData()
	if err != nil {
		config.GetLogger().Warnw("查询头像列表失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))
}

func (s *User) GoPyHandler(c *gin.Context) {
	cont, _ := ioutil.ReadAll(c.Request.Body)

	if cont == nil {
		config.GetLogger().Warnw("信息不能为空",
			"cont:", cont,
		)
		c.JSON(http.StatusOK, s.FailWarp("信息不能为空"))
		return
	}

	user := model.NewUser()

	err, data := user.GetGoPyData(string(cont))
	if err != nil {
		config.GetLogger().Warnw("调用python失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))
}

func (s *User) StatisticsHandler(c *gin.Context) {
	cont, _ := ioutil.ReadAll(c.Request.Body)

	user := model.NewUser()

	err, data := user.GetStatisticsData(string(cont))
	if err != nil {
		config.GetLogger().Warnw("查询统计数据失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))
}

func (s *User) UploadHandler(c *gin.Context) {

	user := model.NewUser()

	err, data := user.GetUploadData(c)
	if err != nil {
		config.GetLogger().Warnw("上传文件失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))
}

func (s *User) FeedbackHandler(c *gin.Context) {
	cont, _ := ioutil.ReadAll(c.Request.Body)

	if cont == nil {
		config.GetLogger().Warnw("反馈不能为空",
			"cont:", cont,
		)
		c.JSON(http.StatusOK, s.FailWarp("反馈不能为空"))
		return
	}

	user := model.NewUser()

	err, data := user.GetFeedbackData(string(cont))
	if err != nil {
		config.GetLogger().Warnw("用户反馈失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))
}

func (s *User) UserCheckFeedbackHandler(c *gin.Context) {
	cont, _ := ioutil.ReadAll(c.Request.Body)

	user := model.NewUser()

	err, data := user.GetUserFeedbackListData(string(cont))
	if err != nil {
		config.GetLogger().Warnw("用户查看反馈失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))
}

func (s *User) CheckReplyHandler(c *gin.Context) {
	cont, _ := ioutil.ReadAll(c.Request.Body)

	user := model.NewUser()

	err, data := user.GetReplyData(string(cont))
	if err != nil {
		config.GetLogger().Warnw("用户查看反馈回复失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))
}

func (s *User) ApplyManagerHandler(c *gin.Context) {
	cont, _ := ioutil.ReadAll(c.Request.Body)

	user := model.NewUser()

	err, data := user.GetApplyData(string(cont))
	if err != nil {
		config.GetLogger().Warnw("用户申请成为管理员失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))
}

func (s *User) DelFileHandler(c *gin.Context) {
	cont, _ := ioutil.ReadAll(c.Request.Body)

	user := model.NewUser()

	err, data := user.GetDelFileData(string(cont))
	if err != nil {
		config.GetLogger().Warnw("用户删除文件失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))
}

func (s *User) MenuHandler(c *gin.Context) {
	userType, _ := strconv.Atoi(c.GetString("type"))

	user := model.NewUser()

	err, data := user.GetMenuData(userType)
	if err != nil {
		config.GetLogger().Warnw("读取菜单失败",
			"err", err.Error(),
		)
		c.JSON(http.StatusOK, s.FailWarp(err.Error()))
		return
	}

	c.JSON(http.StatusOK, s.SuccessWarp(data))
}